<template>
	<view class="warpper">
		<view class="icon-container">
			<image class="logo" src="../../static/image/icon.png"></image>
		</view>
		<view class="input-warpper">
			<input type="text" v-model="form.username" placeholder="请输入用户名" auto-focus="true"/>
			<image src="../../static/image/User.png" class="input-img"></image>
		</view>
		<view class="input-warpper">
			<input type="password" v-model="form.password" placeholder="请输入密码" auto-focus="true"/>
			<image src="../../static/image/Lock.png" class="input-img"></image>
		</view>
		<view class="input-warpper">
			<button :class="form.username&&form.password? 'input-login-active' : 'input-login'" @click="login">登录</button>
		</view>
	</view>
</template>

<script>
    import {  
        mapMutations  
    } from 'vuex';
	import user from '../../http/api/user.js'
	export default {
		data() {
			return {
				form: {
					username: '',
					password: ''
				}
			}
		},
		methods: {
			login() {
				const that = this
				let studentInfo = {
					sname: that.form.username,
					spassword: that.form.password
				}
				if (studentInfo.sname&&studentInfo.spassword) {
					user.login(studentInfo).then(res => {
						if (res.data.code === 200) {
							let data = res.data.data.data;
							let userInfo = {
								hasLogin: true,
								userId: data.studentBean.sid,
								snum: data.studentBean.snum,
								nickName: data.studentBean.sname,
								imgUrl: data.studentBean.spicture,
								sex: data.studentBean.sex,
								clid: data.studentBean.clid,
								cname: data.cname,
								clname: data.clname,
							}
							that.$store.commit("saveUser",userInfo);
							user.semesterInfo().then(res=>{
								let Info = res.data.data.semesterBeans;
								that.$store.commit('saveSemesterInfo',semester);
							});
							user.currentSemester().then(res=>{
								let currentSemester = res.data.data.rSemester;
								let currentWeek = res.data.data.week;
								that.$store.commit('saveCurrentSemester',currentSemester);
								that.$store.commit('saveCurrentWeek',currentWeek);
								uni.setStorage({
									key: "currentWeek",
									data: currentWeek,
									success() {
										uni.switchTab({
											url: '../index/index'
										})
									}
								})
							});
						}else{
							uni.showToast({
								title: res.data.msg,
								icon: 'none',
								duration:1000
							})
						}
					}).catch(err=>{
						uni.showToast({
							title:'登录请求失效，请重试',
							icon:'none',
							duration:1500
						})
					})
				}
			}
		}
	}
</script>

<style>
	.warpper {
		width: 100%;
		height: 100%;
		padding: 20px 0;
		box-sizing: border-box;
	}
	.icon-container{
		width: 80px;
		height: 80px;
		margin: 60px auto 80px;
		border: 1.5px solid #0088FB;
		border-radius: 20%;
		text-align: center;
	}
	.logo {
		width: 60px;
		height: 60px;
		margin-top: 10px;
	}
	.input-warpper{
		width: 70%;
		margin: 5px auto;
		box-sizing: border-box;
    }
	input {
		font-size: 12px;
		width: 100%;
		height: 36px;
		padding-left: 30px;
		border: 1px solid #0088FB;
		border-radius:10px;
		outline: none;
		box-sizing: border-box;
	}
	.input-img{
		position: relative;
		width: 25px;
		height: 25px;
		top: -31px;
		left: 5px;
	}
	.input-login-active{
		width: 100%;
		height: 40px;
        line-height: 40px;
        text-align: center;
        font-size: 18px;
        color: white;
        border-radius: 10px;
        background-color: #0088FB;
    }
	.input-login{
		width: 100%;
		height: 40px;
        line-height: 40px;
        text-align: center;
        font-size: 18px;
        color: white;
        background-color: #7ec4ff;
		border: none;
        border-radius: 10px;
    }

</style>
